package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     * @param userInfo
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {


        //1、解析token
//        Claims claims = JwtUtils.getClaims(token);
//        Integer id = (Integer) claims.get("id");
        Long userId = UserHolder.getUserId();
        //2、向userinfo中设置用户id
        //将int类型装换为long
        userInfo.setId(Long.valueOf(userId));
        //3、调用service
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }
    /**
     * 更新头像
     * @param headPhoto
     * @param id
     * @throws IOException
     */
    @PostMapping("/loginReginfo/head")
    public void updateHead(MultipartFile headPhoto, Long id) throws IOException {
    userInfoService.updateHead(headPhoto, id);
    }
}
